package ipmsg.network;

import ipmsg.IpmsgConstants;
import ipmsg.etc.Command;
import ipmsg.etc.GlobalVar;
import ipmsg.etc.UtilityGlobal;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import java.net.*;
import java.util.Enumeration;

public class UtilityNet {
    public static DatagramSocket msgSocket;

    private static void sendUdpPacket(Command com, String ip) {
        byte[] bText = com.exportToBuf();
        try {
            DatagramPacket sendPacket = new DatagramPacket(bText, bText.length, InetAddress.getByName(ip),
                    IpmsgConstants.DEFAULT_PORT);

            msgSocket.send(sendPacket);
        } catch (IOException ex) {
        }
    }

    public static void sendUdpPacket(Command com) {
        sendUdpPacket(com, com.getIp());
    }

    public static void broadcastUdpPacket(Command com) {
        sendUdpPacket(com, "255.255.255.255");
        for (int i = 0; i < GlobalVar.ADDED_USER.size(); i++) {
            if (!GlobalVar.ALL_IP_ADDRESS.contains(GlobalVar.ADDED_USER.get(i))) {
                sendUdpPacket(com, (String) GlobalVar.ADDED_USER.get(i));
            }
        }
    }

    public static void refreshMyIps() {
        GlobalVar.ALL_IP_ADDRESS.clear();
        try {
            Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
            while (netInterfaces.hasMoreElements()) {
                NetworkInterface networkInterface = netInterfaces.nextElement();
                Enumeration<InetAddress> ips = networkInterface.getInetAddresses();
                while (ips.hasMoreElements()) {
                    String tmpIp = ips.nextElement().getHostAddress();
                    if ((UtilityGlobal.isIP(tmpIp)) && (!tmpIp.startsWith("127"))) {
                        GlobalVar.ALL_IP_ADDRESS.add(tmpIp);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static boolean checkPort() {
        try {
            new DatagramSocket(IpmsgConstants.DEFAULT_PORT).close();
            return true;
        } catch (SocketException ex) {
        }
        return false;
    }

    public static String readDelimiter(DataInputStream inputStream, int bufSize) {
        byte[] res = new byte[bufSize];
        int count = 0;
        try {
            while (count < bufSize - 1) {
                inputStream.read(res, count, 1);

                if (res[count] == 58) {
                    break;
                }
                count++;
            }
            res[count] = 0;
            return new String(res, GlobalVar.CHARACTER_ENCODING).trim();
        } catch (IOException ex) {
        }
        return null;
    }

    public static String readDelimiter(Socket socket, int bufSize) {
        try {
            return readDelimiter(new DataInputStream(new BufferedInputStream(socket.getInputStream())), bufSize);
        } catch (IOException ex) {
        }

        return null;
    }
}
